 
package com.pactera.config.security;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;

/**
 * 用户未登录时返回给前端的数据
 */
@Component
public class ApiAuthenticationEntryPoint implements AuthenticationEntryPoint {
	
	private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        logger.info("###### Not Login!");
        
        String respJson = JSON.toJSONString(AuthResultVO.result(AuthResultEnum.NEED_AUTHORITIES,false));
        
        logger.info("###### Login Resp:{}", respJson);
        
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(respJson);
    }
}
